package manager;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import bean.Promotion;
import bean.User;

import utils.DBService;

public class UserManager{
	private static UserManager uniqueInstance=null;
	
	private Statement stmt;
	private ResultSet rs;
	private Connection con;
	
	public static UserManager getInstance(){
		if(uniqueInstance==null){
			uniqueInstance=new UserManager();
		}
		return uniqueInstance;
	}
	
	private UserManager(){
		con=DBService.getConnection();
	}
	
	private boolean isValidUser(String userName,String password){
		User user=this.getUserByUserName(userName);
		if(user!=null){
			return user.getPassword().equals(password);
		}
		return false;
	}
	
	private int getUserType(String userName){
		User user=this.getUserByUserName(userName);
		if(user!=null){
			return user.getType();
		}
		return 0;
	}
	
	public boolean isAdmin(String userName,String password){
		return isValidUser(userName,password)&&(getUserType(userName)==4);
	}
	
	public boolean isGeneralManager(String userName, String password) {
		return isValidUser(userName,password)&&(getUserType(userName)==2);
	}
	
	public boolean isSalesManager(String userName, String password) {
		return isValidUser(userName,password)&&(getUserType(userName)==3);
	}
	
	public boolean isCustomer(String userName, String password) {
		return isValidUser(userName,password)&&(getUserType(userName)==1);
	}
	
	private User getUserByUserName(String userName){
		User user=null;
		try {
			stmt=con.createStatement();
			String sql="SELECT * FROM user WHERE UserName='"+userName+"'";
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				user=new User();
				user.setUserId(rs.getInt("UserId"));
				user.setUserName(rs.getString("UserName"));
				user.setPassword(rs.getString("Password"));
				user.setType(rs.getInt("Type"));
				user.setSex(rs.getString("Sex"));
				user.setAddress(rs.getString("Address"));
				user.setPhone(rs.getString("Phone"));
				user.setPost(rs.getString("Post"));
				user.setEmail(rs.getString("Email"));
				user.setScore(rs.getInt("Score"));
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	public String[][] getUserData(int userType, int keywordType, String keyword) {
		ArrayList<User> userList=new ArrayList();
		String kt=null;
		if(keywordType==0){
			kt="UserName";
		}
		try {
			stmt=con.createStatement();
			String sql=null;
			if(keyword.equals("")){
				if(userType!=0){
					sql="SELECT * FROM user WHERE Type='"+userType+"'";
				}else{
					sql="SELECT * FROM user WHERE Type!=4";
				}
			}else{
				if(userType!=0){
					sql="SELECT * FROM user WHERE Type='"+userType+"' AND "+kt+"='"+keyword+"'";
				}else{
					sql="SELECT * FROM user WHERE "+kt+"='"+keyword+"'"+" AND Type!=4";
				}
			} 
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				User user=new User();
				user.setUserId(rs.getInt("UserId"));
				user.setUserName(rs.getString("UserName"));
				user.setPassword(rs.getString("Password"));
				user.setType(rs.getInt("Type"));
				user.setSex(rs.getString("Sex"));
				user.setAddress(rs.getString("Address"));
				user.setPhone(rs.getString("Phone"));
				user.setPost(rs.getString("Post"));
				user.setEmail(rs.getString("Email"));
				user.setScore(rs.getInt("Score"));
				userList.add(user);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data=new String[userList.size()][2];
		for(int i=0;i<userList.size();i++){
			data[i][0]=userList.get(i).getUserName();
			String s=null;
			switch(userList.get(i).getType()){
			case 1:
				s="顾客";
				break;
			case 2:
				s="总经理";
				break;
			case 3:
				s="销售经理";
				break;
			}
			data[i][1]=s;
		}
		return data;
	}
	
	public boolean signUp(User newUser){
		if(this.getUserByUserName(newUser.getUserName())==null){
			this.insertUser(newUser);
			return true;
		}
		return false;
	}

	private void insertUser(User user) {
		String sql="INSERT INTO user (UserName,Password,Type,Sex,Address,Phone,Post,Email,Score) VALUES('"+user.getUserName()+"','"+user.getPassword()+"',"+user.getType()+",'"+user.getSex()+"','"+user.getAddress()+"','"+user.getPhone()+"','"+user.getPost()+"','"+user.getEmail()+"',"+user.getScore()+")";
		this.processSqlUpdate(sql);	
	}

	public void updateUser(User user) {
		String sql="UPDATE user Set Password='"+user.getPassword()+"',Sex='"+user.getSex()+"',Phone='"+user.getPhone()+"',Address='"+user.getAddress()+"',Email='"+user.getEmail()+"',Post='"+user.getPost()+"',Score="+user.getScore()+" WHERE UserName='"+user.getUserName()+"'";
		this.processSqlUpdate(sql);	
	}
	
	public void updateUserPassword(User user) {
		String sql="UPDATE user Set Password='"+user.getPassword()+"' WHERE UserName='"+user.getUserName()+"'";
		this.processSqlUpdate(sql);	
	}
	
	public void deleteUser(String userName){
		String sql="DELETE FROM user WHERE UserName='"+userName+"'";
		this.processSqlUpdate(sql);
	}
	
	private void processSqlUpdate(String sql) {
		try {
			stmt=con.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}



}
